package com.aplikata.evok.dao;

import java.util.Date;

import org.springframework.stereotype.Repository;

import com.aplikata.bo.City;
import com.aplikata.constants.ConstantsMsg;
import com.aplikata.dao.PublicDaoImpl;
import com.aplikata.evok.bo.Patient;
import com.aplikata.exception.CustomException;
import com.aplikata.util.FacesUtil;
import com.aplikata.util.StringUtil;
import com.aplikata.util.YunDateUtil;

@Repository("patientDao")
public class PatientDaoImpl extends PublicDaoImpl<Patient, Long> implements PatientDao {

	@Override
	public boolean validate(Patient patient) throws Exception {
		if (patient == null)
			return false;

		Long tempId = patient.getId();
		String surname = patient.getSurname();
		String name = patient.getName();
		Date birthday = patient.getBirthday();
		City birthCity = patient.getBirthplace();
		Integer sex = patient.getSex();
//		String jmbg = patient.getJmbg();
//		String oib = patient.getOib();
		
		if (StringUtil.isStrEmpty(surname)) {
			throw new CustomException(ConstantsMsg.MSG_REQUIRE,
					new Object[] { FacesUtil.getMessage(ConstantsMsg.SURNAME) });
		}

		if (StringUtil.isStrEmpty(name)) {
			throw new CustomException(ConstantsMsg.MSG_REQUIRE,
					new Object[] { FacesUtil.getMessage(ConstantsMsg.NAME) });
		}

		if (birthday == null) {
			throw new CustomException(ConstantsMsg.MSG_REQUIRE,
					new Object[] { FacesUtil.getMessage(ConstantsMsg.BIRTHDAY) });
		}

		if (birthCity == null) {
			throw new CustomException(ConstantsMsg.MSG_REQUIRE,
					new Object[] { FacesUtil.getMessage(ConstantsMsg.BIRTHPLACE) });
		}
		
		if (sex == null) {
			throw new CustomException(ConstantsMsg.MSG_REQUIRE,
					new Object[] { FacesUtil.getMessage("empl_sex") });
		}

//		if (StringUtil.isStrEmpty(patient.getStreet())) {
//			throw new CustomException(ConstantsMsg.MSG_REQUIRE,
//					new Object[] { FacesUtil.getMessage(ConstantsMsg.ADDRESS) });
//		}

//		if (patient.getCity() == null) {
//			throw new CustomException(ConstantsMsg.MSG_REQUIRE,
//					new Object[] { FacesUtil.getMessage(ConstantsMsg.CITY) });
//		}
//
//		if (patient.getCreateDate() == null) {
//			throw new CustomException(ConstantsMsg.MSG_REQUIRE,
//					new Object[] { FacesUtil.getMessage(Patient.LB_CREATE_DATE) });
//		}
//
//		if (FacesUtil.isKeyNull(tempId)) {
//			if (patient.getSendEmail().booleanValue() == true
//					&& StringUtil.isStrEmpty(contact.getEmail1())) {
//				throw new CustomException(ConstantsMsg.MSG_REQUIRE,
//						new Object[] { FacesUtil.getMessage("default_email") });
//			}
//		}
		
//		if(!StringUtil.isStrEmpty(jmbg)){
//			if(AplikataUtil.validateJMBG(jmbg)){
//				String hql = "from Patient p where p.jmbg ='" + jmbg + "'";
//				if (!FacesUtil.isKeyNull(tempId)) {
//					hql += " and p.id != " + tempId;
//				}
//				List list = this.find(hql);
//				if(list != null && !list.isEmpty()){
//					throw new CustomException("error_exist", new Object[] { jmbg });
//				}
//			}else{
//				throw new CustomException("default_error_number", new Object[] { FacesUtil.getMessage("default_jmbg") });
//			}
//		}
//		
//		if(!StringUtil.isStrEmpty(oib)){
//			if(AplikataUtil.validateOIB(oib)){
//				String hql = "from Patient p where p.oib ='" + oib + "'";
//				if (!FacesUtil.isKeyNull(tempId)) {
//					hql += " and p.id != " + tempId;
//				}
//				List list = this.find(hql);
//				if(list != null && !list.isEmpty()){
//					throw new CustomException("error_exist", new Object[] { oib });
//				}
//			}else{
//				throw new CustomException("default_error_number", new Object[] { FacesUtil.getMessage("default_oib") });
//			}
//		}

		String hql = " from Patient p where p.surname = '" + surname + "'" + " and p.name = '" + name + "'"
				+ " and p.birthday = '" + YunDateUtil.formatDate(birthday) + "'" + " and p.birthplace.id = "
				+ birthCity.getId() + " and p.sex = " + sex;

		if (!FacesUtil.isKeyNull(tempId)) {
			hql += " and p.id != " + tempId;
		}

		Patient old = (Patient) this.getFirstRow(hql);

		if (old != null) {
			throw new CustomException("error_exist", new Object[] { surname + " " + name });
		}

		return true;
	}
}
